package top.chendawei.system.service.impl;

import org.apache.commons.collections.CollectionUtils;
import org.hibernate.query.Query;
import org.springframework.stereotype.Service;
import top.chendawei.aop.localcache.GuavaLocalCache;
import top.chendawei.system.beans.entity.SysBlacklist;
import top.chendawei.system.dao.HibernateDaoSupport;
import top.chendawei.system.service.IBlacklistService;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BlacklistServiceImpl extends HibernateDaoSupport implements IBlacklistService {

    @Override
    public void save(SysBlacklist m) {
        super.getCurrentSession().saveOrUpdate(m);
    }

    @Override
    @GuavaLocalCache
    public List<String> queryBlacklist(long minTime) {
        String hql = "from SysBlacklist  where time>:minTime";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("minTime", minTime);
        return ((List<SysBlacklist>) query.list()).stream().map(SysBlacklist::getToken).collect(Collectors.toList());
    }

    @Override
    @GuavaLocalCache(keyExt = "#token")
    public boolean checkContain(String token) {
        String hql = "from SysBlacklist  where token=:token";
        Query query = getCurrentSession().createQuery(hql);
        query.setParameter("token", token);
        List ll = query.list();
        if (CollectionUtils.isNotEmpty(ll)) {
            return true;
        }
        return false;
    }
}
